import requests
import os

def get_web_content(url):
	try:
		kv = {'user-agent':'Mozilla/5.0'}
		r = requests.get(url, headers = kv)
		r.raise_for_status()
		r.encoding = r.apparent_encoding
		print(r.text[:2000])
	except:
		return "出现异常"

'''
百度搜索引擎
'''
def get_baidu_search():
	searchUrl ='https://www.baidu.com/s'
	kv ={'wd':'python'}
	try:
		r = requests.get(searchUrl, params=kv)
		r.raise_for_status()
		r.encoding = r.apparent_encoding
		print(len(r.text))
		print(r.text[:1000])
	except:
		return '出现异常'

jd_goods_url = 'https://item.jd.com/3352172.html?cpdad=1DLSUE'
amazon_good_url = 'https://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B01ARKEV1G'
#get_web_content(jd_goods_url) #获得京东商品信息
#get_web_content(amazon_good_url) #获取亚马逊信息
#get_baidu_search()

def get_images(url):
	picname = url.split('/')[-1]
	picdir = 'D:\\Download\\'
	path = picdir+picname
	if  not os.path.exists(picdir):
		os.mkdir(picdir)
	if os.path.exists(path):
		print("图片已下载")
		return
	
	try:
		r = requests.get(url)
		with open(path, 'wb') as f:
			f.write(r.content)
			f.close()
		return
	except:
		print("图片无法保存")


get_images('http://img.ivsky.com/img/tupian/pre/201612/12/qingrenjie_meigui_liwu-003.jpg')